package io.renren.com.qywt.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.renren.com.qywt.entity.AftersaleWorkOrderEntity;
import io.renren.com.qywt.service.AftersaleProdDetailService;
import io.renren.commons.mybatis.service.impl.CrudServiceImpl;
import io.renren.com.qywt.dao.AftersaleWorkOrderDao;
import io.renren.com.qywt.dto.AftersaleWorkOrderDTO;
import io.renren.com.qywt.service.AftersaleWorkOrderService;
import io.renren.commons.security.context.TenantContext;
import io.renren.commons.security.user.SecurityUser;
import io.renren.commons.security.user.UserDetail;
import io.renren.commons.tools.constant.Constant;
import io.renren.commons.tools.enums.SuperAdminEnum;
import io.renren.commons.tools.enums.SuperTenantEnum;
import io.renren.commons.tools.page.PageData;
import io.renren.feign.UserFeignClient;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * 供应商工单表
 *
 * @author jiaohj 15110414840@139.com
 * @since 3.0 2022-08-20
 */
@Service
@AllArgsConstructor
public class AftersaleWorkOrderServiceImpl extends CrudServiceImpl<AftersaleWorkOrderDao, AftersaleWorkOrderEntity, AftersaleWorkOrderDTO> implements AftersaleWorkOrderService {
    private final UserFeignClient userFeignClient;
    @Autowired
    private AftersaleProdDetailService prodDetailService;
    @Override
    public QueryWrapper<AftersaleWorkOrderEntity> getWrapper(Map<String, Object> params){
        QueryWrapper<AftersaleWorkOrderEntity> wrapper = new QueryWrapper<>();
        String orderNo = (String)params.get("orderNo");
        wrapper.eq(StringUtils.isNotBlank(orderNo), "order_no", orderNo);
        String platformType = (String)params.get("platformType");
        wrapper.eq(StringUtils.isNotBlank(platformType), "platform_type", platformType);
        String deliveryNo = (String)params.get("deliveryNo");
        wrapper.eq(StringUtils.isNotBlank(deliveryNo), "delivery_no", deliveryNo);
        String problemType = (String)params.get("problemType");
        wrapper.eq(StringUtils.isNotBlank(problemType), "problem_type", problemType);
        wrapper.eq("tenant_code", TenantContext.getTenantCode(SecurityUser.getUser()));
        return wrapper;
    }

    @Override
    public PageData<AftersaleWorkOrderDTO> page(Map<String, Object> params) {
        System.out.println("page1=================");
        //分页
        IPage<AftersaleWorkOrderEntity> page = getPage(params, "create_date",false);
        System.out.println(page.toString());
        //查询
        List<AftersaleWorkOrderEntity> list = baseDao.getList(getQueryParams(params));
        for(AftersaleWorkOrderEntity entity:list){
            entity.setProdDetailDTOList(prodDetailService.getByWorkOrderId(entity.getId()));
        }
        return getPageData(list, page.getTotal(),AftersaleWorkOrderDTO.class);
    }

    private Map<String, Object> getQueryParams(Map<String, Object> params){
        //普通管理员，只能查询所属部门及子部门的数据
        UserDetail user = SecurityUser.getUser();
        if(user.getSuperAdmin() == SuperAdminEnum.NO.value() && user.getSuperTenant() == SuperTenantEnum.NO.value()) {
            params.put("deptIdList", userFeignClient.getSubDeptIdList(user.getDeptId()).getData());
        }
        //租户
        params.put(Constant.TENANT_CODE, TenantContext.getTenantCode(user));
        return params;
    }


}